#include <bits/stdc++.h>

using namespace std;

struct TreeNode
{
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};

class Solution
{
public:
    vector<int> traverse(TreeNode *root)
    {
        vector<int> res;
        if (root == nullptr)
        {
            return res;
        }
        queue<TreeNode *> que;
        que.push(root);
        while (!que.empty())
        {
            int size = que.size();
            for (int i = 0; i < size; i++)
            {
                TreeNode *cur = que.front();
                que.pop();
                res.push_back(cur->val);
                if (cur->left)
                {
                    que.push(cur->left);
                }
                if (cur->right)
                {
                    que.push(cur->right);
                }
            }
        }
        return res;
    }

    TreeNode *recursion(vector<int> &preorder, int preorder_start, int preorder_end, vector<int> &inorder, int inorder_start, int inorder_end)
    {
        if (preorder_end - preorder_start == 0)
        {
            return nullptr;
        }
        TreeNode *root = new TreeNode(preorder[preorder_start]);
        if (preorder_end - preorder_start == 1)
        {
            return root;
        }

        int index = 0;
        for (int i = 0; i < inorder_end; i++)
        {
            if (inorder[i] == preorder[preorder_start])
            {
                index = i;
                break;
            }
        }

        int left_inorder_start = inorder_start;
        int left_inorder_end = index;
        int right_inorder_start = index + 1;
        int right_inorder_end = inorder_end;

        int left_preorder_start = preorder_start + 1;
        int left_preorder_end = preorder_start + 1 + index - inorder_start;
        int right_preorder_start = preorder_start + 1 + index - inorder_start;
        int right_preorder_end = preorder_end;

        root->left=recursion(preorder,left_preorder_start,left_preorder_end,inorder,left_inorder_start,left_inorder_end);
        root->right=recursion(preorder,right_preorder_start,right_preorder_end,inorder,right_inorder_start,right_inorder_end);

        return root;
    }

    TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder)
    {
        if (preorder.size() == 0 || inorder.size() == 0)
        {
            return nullptr;
        }
        return recursion(preorder, 0, preorder.size(), inorder, 0, inorder.size());
    }
};

int main()
{
    vector<int> inorder{3, 9, 20, 15, 7};
    vector<int> postorder{9, 3, 15, 20, 7};
    Solution sol;
    TreeNode *root = sol.buildTree(inorder, postorder);
    vector<int> res = sol.traverse(root);
    for (auto num : res)
    {
        cout << num << " ";
    }
    cout << endl;
    return 0;
}